package net.java.mediasystems.database.core.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;

@Entity
@Table(name = "PICTURE")
public class Picture implements Serializable {

	private static final long serialVersionUID = -6698885216529673046L;
	
	private static final String MAX_FILENAME_LENGTH = "50";
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID")
	private Long id;
	
	@Column(name ="NAME", nullable = false)
	private String name;
	
	@Column(name = "CONTENT_TYPE", nullable = false)
	private String contentType;
	
	@Column(name ="PICTURE_BYTES", nullable = false, columnDefinition = "blob(16777215)")
	private byte[] pictureBytes;
	
	@Column(name ="COVER_PICTURE_FOR_MEDIA_ITEM", nullable = false)
	private boolean coverPictureForMediaItem;
	
	@Column(name = "INSERTED_DATE")
	private Date insertedDate;
	
	@Column(name = "UPDATED_DATE")
	private Date updatedDate;
	
	@PrePersist
	private void onCreate() {
		this.insertedDate = new Date();
	}
	
	@PreUpdate
	private void onUpdate() {
		this.updatedDate = new Date();
	}
	
	@Override
	public int hashCode() {
		final int prime = 43;
		int result = 1;
		
		result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Picture other = (Picture) obj;
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		return true;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public byte[] getPictureBytes() {
		return pictureBytes;
	}

	public void setPictureBytes(byte[] pictureBytes) {
		this.pictureBytes = pictureBytes;
	}
	
	public boolean isCoverPictureForMediaItem() {
		return coverPictureForMediaItem;
	}
	
	public void setCoverPictureForMediaItem(boolean coverPictureForMediaItem) {
		this.coverPictureForMediaItem = coverPictureForMediaItem;
	}

	public Date getInsertedDate() {
		return insertedDate;
	}

	public Date getUpdatedDate() {
		return updatedDate;
	}
}
